package com.foxzzz.datamemory.service;

import com.foxzzz.datamemory.entity.BookNote;
import com.foxzzz.datamemory.repository.BookNoteRepository;
import com.foxzzz.datamemory.util.SnowflakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BookNoteService {
    private final static String FINISH = "FINISH";
    private final static String START = "START";
    @Autowired
    private BookNoteRepository repository;
    private final static double[] levelArray = new double[]{0.5, 12, 24, 48, 96, 168, 360};

    private Date getDateFromLevel(Date date, Integer level) {
        return new Date(date.getTime() + (long) levelArray[level] * 1000 * 60);
    }

    /**
     * 保存或者更新记录信息
     *
     * @param bookNote
     * @return
     */
    public BookNote save(BookNote bookNote) {
        if (null == bookNote.getNoteId()) {
            bookNote.setNoteId(SnowflakeUtil.getIdString());
            Date date = new Date();
            bookNote.setDateCreate(date);
            bookNote.setLastReviewDate(date);
            bookNote.setNextReviewDate(getDateFromLevel(date, 0));
            bookNote.setReviewLevel(0);
            bookNote.setFinishTimes(0);
            bookNote.setState(START);
            if (bookNote.getOrders() == 0) {
                bookNote.setOrders(date.getTime());
            }
        }
        return repository.save(bookNote);
    }

    /**
     * 推到下一个记忆曲线的等级
     *
     * @param id
     * @return
     */
    public BookNote levelUp(String id) {
        BookNote one = repository.getOne(id);
        if (one == null) {
            throw new RuntimeException("记录不存在");
        }
        int reviewLevel = one.getReviewLevel();
        if (reviewLevel >= levelArray.length) {
            one.setFinishTimes(one.getFinishTimes() + 1);
            one.setState(FINISH);
            reviewLevel = 0;
        } else {
            reviewLevel += 1;
        }
        Date date = new Date();
        one.setLastReviewDate(date);
        one.setReviewLevel(reviewLevel);
        one.setNextReviewDate(getDateFromLevel(date, reviewLevel));
        return repository.save(one);
    }

    /**
     * 按条件 分页查询bookNote
     *
     * @param bookNote
     * @param page
     * @param size
     * @param order
     * @return
     */
    public Page<BookNote> page(BookNote bookNote, int page, int size, String order) {
        Sort by = "asc".equals(order) ? Sort.by(Sort.Order.asc("orders")) : Sort.by(Sort.Order.desc("orders"));
        PageRequest of = PageRequest.of(page, size, by);
        return bookNote == null ? repository.findAll(of) : repository.findAll(Example.of(bookNote), of);
    }

    /**
     * 分页查出所有需要复习的
     *
     * @return
     */
    public Page<BookNote> listNeedReview(int page, int size) {
        return repository.findAllNeedVisit(new Date(), PageRequest.of(page, size, Sort.by(Sort.Order.asc("bookId"), Sort.Order.desc("id"))));
    }

    /**
     * 删除评论信息
     *
     * @param id
     */
    public void delete(String id) {
        repository.deleteById(id);
    }

    /**
     * 撤回到LEVEL 0 并且重新开启
     *
     * @param id
     * @return
     */
    public BookNote reOpen(String id) {
        BookNote one = repository.getOne(id);
        if (one == null) {
            throw new RuntimeException("记录不存在");
        }
        Date date = new Date();
        one.setLastReviewDate(date);
        one.setReviewLevel(0);
        one.setNextReviewDate(getDateFromLevel(date, 0));
        one.setState(START);
        return repository.save(one);
    }
}
